package com.baizhi.config.log;

import cn.hutool.json.JSONUtil;
import com.baizhi.entity.CmfzAdmin;
import com.baizhi.entity.CmfzLog;
import com.baizhi.mapper.CmfzLogMapper;
import com.baizhi.utils.IPKit;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.lang.reflect.Method;
import java.util.Date;

@Configuration
@Aspect
public class LogAspect {

    @Autowired
    private CmfzLogMapper cmfzLogMapper;

    @Autowired
    private JmsTemplate jmsTemplate;

    @After("@annotation(com.baizhi.config.log.LogAnnotation)")
    public void logAop(JoinPoint joinPoint){
        /**
         * 1.日志信息采集
         * 管理员的名字
         * ip  通过 Request 对象获取
         * 时间
         * 操作内容和类型
         */
        CmfzLog cmfzLog = new CmfzLog();

        cmfzLog.setLogTime(new Date());

        /**
         * 获取 ip
         *
         * RequestContextHolder 可以获取当前线程对象的请求
         *
         * 一般都通过工具类
         */
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();

        HttpServletRequest request = requestAttributes.getRequest();

        HttpSession session = request.getSession();
        CmfzAdmin admin = (CmfzAdmin) session.getAttribute("admin");

        if (admin == null) {
            cmfzLog.setUsername("xxxxx");
        }else {
            cmfzLog.setUsername(admin.getUsername());
        }

        String addr = IPKit.getIpAddrByRequest(request);
        cmfzLog.setLogIp(addr);

        /**
         *  通过自定义注解
         *  获取注解的值  连接点对象
         *  1.通过连接点对象获取方法签名对象
         *  2 通过方法签名对象 获取方法对象
         *  3 获取方法上的注解
         *
         */

        MethodSignature signature = (MethodSignature)joinPoint.getSignature();
        Method method = signature.getMethod();
        LogAnnotation annotation = method.getAnnotation(LogAnnotation.class);
        String content = annotation.content();
        String type = annotation.type();

        cmfzLog.setLogContent(content);
        cmfzLog.setLogType(type);

        String jsonStr = JSONUtil.toJsonStr(cmfzLog);
        System.out.println(jsonStr+"要发送到队列中了");
        jmsTemplate.send("cmfz-log", new MessageCreator() {
            @Override
            public Message createMessage(Session session) throws JMSException {
                return session.createTextMessage(jsonStr);
            }
        });


        /**
         * 向数据库中插入数据
         */
        //cmfzLogMapper.insert(cmfzLog);
    }
}
